package com.stj.servlet;

import com.google.gson.Gson;
import com.stj.entity.Blog;
import com.stj.entity.Link;
import com.stj.entity.blogType;
import com.stj.service.AdminBlogService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

@WebServlet("/admin/adminBlogServlet")
public class AdminBogServlet extends HttpServlet {
    AdminBlogService service= new AdminBlogService();
    Gson gson = new Gson();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String way = req.getParameter("way");
        if(way.equals("blogManage")){
            this.getBlogManage(req,resp);
        }else if(way.equals("deleteBlog")){
            this.deleteBlog(req,resp);
        }else if(way.equals("updateFirst")){
            this.updateBlogFirst(req,resp);
        }else if(way.equals("update")){
            this.updateBlog(req,resp);
        }else if(way.equals("blogTypeManage")){
            this.blogTypeManage(req,resp);
        }else if(way.equals("getCommentByState")){
            this.getCommentByState(req,resp);
        }else if(way.equals("reviewComment")){
            this.reviewComment(req,resp);
        }else if(way.equals("deleteComment")){
            this.deleteComment(req,resp);
        }else if(way.equals("getAllLink")){
            this.getAllLink(req,resp);
        }else if(way.equals("modifyPassword")){
            this.modifyPassword(req,resp);
        }else if(way.equals("addBlogType")){
            this.addBlogType(req,resp);
        }else if(way.equals("updateBlogType")){
            this.updateBlogType(req,resp);
        }else if(way.equals("deleteBlogTypeByIds")){
            this.deleteBlogTypeByIds(req,resp);
        }
    }
    /*博客类别管理页面 删除*/
    private void deleteBlogTypeByIds(HttpServletRequest req, HttpServletResponse resp)throws  IOException,ServletException {
        String ids = req.getParameter("ids");
        List<Integer> delIds = new ArrayList<>();
        String[] split = ids.split(",");
        for (String idStr : split) {
            int id = Integer.parseInt(idStr);
            delIds.add(id);
        }
        int result = service.deleteBlogTypeByIds(delIds);
        String json = null;
        if (result > 0){
            json = gson.toJson("success");
        }else {
            json = gson.toJson("fail");
        }
        resp.getWriter().write(json);
    }

    /*博客类别管理页面 修改*/
    private void updateBlogType(HttpServletRequest req, HttpServletResponse resp)throws  IOException,ServletException {
        int id = Integer.parseInt(req.getParameter("id"));
        String typeName = req.getParameter("typeName");
        int orderNo = Integer.parseInt(req.getParameter("orderNo"));
        Gson gson = new Gson();
        blogType blogType = new blogType(id,typeName,orderNo);
        int result = service.updateBlogTypeById(blogType);
        String json = null;
        if (result > 0){
            json = gson.toJson("success");
        }else {
            json = gson.toJson("fail");
        }
        resp.getWriter().write(json);
    }

    /*博客类别管理页面 添加*/
    private void addBlogType(HttpServletRequest req, HttpServletResponse resp) throws  IOException,ServletException {
        String typeName = req.getParameter("typeName");
        int orderNo = Integer.parseInt(req.getParameter("orderNo"));
        Gson gson = new Gson();
        int result = service.addBlogType(typeName,orderNo);
        String json = null;
        if (result > 0){
            json = gson.toJson("success");
        }else {
            json = gson.toJson("fail");
        }
        resp.getWriter().write(json);
    }

    /*后台修改密码*/
    private void modifyPassword(HttpServletRequest req, HttpServletResponse resp) throws  IOException,ServletException {
        String userName = req.getParameter("userName");

        String newPassword = req.getParameter("newPassword");
        String newPassword2 = req.getParameter("newPassword2");
        String json = null;
        if (newPassword.equals(newPassword2)){
            int result = service.updateBlogPassword(userName,newPassword);
            if (result>0){
                json = gson.toJson("success");
            }else {
                json = gson.toJson("fail");
            }
        }else {
            json = gson.toJson("fail");
        }
        resp.getWriter().write(json);
    }

    /*后台链接*/
    private void getAllLink(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException {
        List<Link> linkList = service.selectAllLink();
        HttpSession session = req.getSession();
        session.setAttribute("linkList",linkList);
        req.getRequestDispatcher("/admin/linkManage.jsp").forward(req,resp);
    }


    /* 后台删除评论*/
    private void deleteComment(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException {
        String ids = req.getParameter("ids");
        String json = null;
        List<Integer> del_ids = new ArrayList<>();
        String[] str_id = ids.split(",");
        for (String idStr : str_id) {
            int id = Integer.parseInt(idStr);
            del_ids.add(id);
        }
        int result = service.deleteCommentByIds(del_ids);
        if (result>0){
            json = gson.toJson("success");
        }else {
            json = gson.toJson("fail");
        }
        resp.getWriter().write(json);
    }

    private void reviewComment(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException {
        String ids = req.getParameter("ids");
        int state = Integer.parseInt(req.getParameter("state"));
        String json = null;
        List<Integer> del_ids = new ArrayList<>();
        String[] str_id = ids.split(",");
        //组装id的集合
        for (String idStr : str_id) {
            int id = Integer.parseInt(idStr);
            del_ids.add(id);
        }
        if (state == 1){
            //审核通过
            //状态设为1
            int result = service.setCommentStateOne(del_ids);
            if (result>0){
                json = gson.toJson("success");
            }else {
                json = gson.toJson("fail");
            }
        }else {
            //审核不通过
            int result = service.setCommentStateTwo(del_ids);
            if (result>0){
                json = gson.toJson("success");
            }else {
                json = gson.toJson("fail");
            }
        }
        resp.getWriter().write(json);
    }

    /*评论审核*/
    private void getCommentByState(HttpServletRequest req, HttpServletResponse resp) throws IOException,ServletException {
        int state = Integer.parseInt(req.getParameter("state"));
        HttpSession session = req.getSession();
        if (state == 0){
            List commentZero = service.selectCommentByState(state);
            session.setAttribute("commentByZero",commentZero);
            req.getRequestDispatcher("/admin/commentReview.jsp").forward(req,resp);
        }else {
            List commentOne = service.selectCommentByState(state);
            session.setAttribute("commentByOne",commentOne);
            req.getRequestDispatcher("/admin/commentManage.jsp").forward(req,resp);
        }
    }

    /*博客类别管理*/
    private void blogTypeManage(HttpServletRequest req, HttpServletResponse resp)  throws  IOException, ServletException{
        List  blogTypes = service.getAllBlogType();
        HttpSession session = req.getSession();
        session.setAttribute("blogTypeCountList",blogTypes);
        req.getRequestDispatcher("/admin/blogTypeManage.jsp").forward(req,resp);
    }

    private void updateBlog(HttpServletRequest req, HttpServletResponse resp) throws  IOException,ServletException {
        String id = req.getParameter("id");
        String title = req.getParameter("title");
        String blogTypeID = req.getParameter("blogType.id");
        String content = req.getParameter("content");
        String summary = req.getParameter("summary");
        Blog blog =new Blog( Integer.parseInt(id),  title,  summary,  content,  Integer.parseInt(blogTypeID));
        int i =service.updateBlog(blog);
        if(i>0){
            String json = gson.toJson("success");
            resp.getWriter().write(json);
        }

    }


    private void updateBlogFirst(HttpServletRequest req, HttpServletResponse resp) throws  IOException,ServletException {
        String id = req.getParameter("id");
        Blog updateBlogFirst = service.updateBlogFirst(Integer.parseInt(id));
        HttpSession session = req.getSession();
        session.setAttribute("updateBlogFirst",updateBlogFirst);
        req.getRequestDispatcher("/admin/modifyBlog.jsp").forward(req,resp);
    }

    private void deleteBlog(HttpServletRequest req, HttpServletResponse resp) throws  IOException,ServletException {
        String id =req.getParameter("ids");
        int i = service.deleteBlog(Integer.parseInt(id));
        if(i>0){
            String json = gson.toJson("success");
            resp.getWriter().write(json);
        }

    }

    private void getBlogManage(HttpServletRequest req, HttpServletResponse resp) throws  IOException,ServletException {
        List getBlogAndBlogTypeList = service.getBlogAndBlogTypeList();
        HttpSession session = req.getSession();
        session.setAttribute("getBlogAndBlogTypeList",getBlogAndBlogTypeList);
        req.getRequestDispatcher("/admin/blogManage.jsp").forward(req,resp);
    }
}
